//
//  PlaceAnOrderController.m
//  UcarTest
//
//  Created by 陈曦 on 16/1/25.
//  Copyright © 2016年 chenxi. All rights reserved.
//

#import "PlaceAnOrderController.h"
#import "Define.h"
#import "DIYCarDetailButton.h"
#import "PaymentViewController.h"
#import "NetRequestGroup.h"
#import "CarDetailForgeViewController.h"
#import "AFNetworking.h"
#import "NetRequestGroup.h"
#import "Access_token.h"
#import "MBProgressHUD.h"

@interface PlaceAnOrderController ()

@property (nonatomic, strong) UIScrollView *backgroundScrollView;

@property (nonatomic, strong) DIYCarDetailButton *carDetailBtn;

@property (nonatomic, assign) NSTimeInterval d_valueForTime;

@property (nonatomic, assign) NSTimeInterval checkOutTimeStamp;

@property (nonatomic, assign) NSTimeInterval giveBackTimeStamp;

@property (nonatomic, assign) NSInteger fateD_Value;

@end

@implementation PlaceAnOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //导航条
    self.navigationController.navigationBarHidden = NO;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont boldSystemFontOfSize:18];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"下单";
    self.navigationItem.titleView = titleLabel;
    self.navigationController.navigationBar.barTintColor = mainColor;
    self.view.backgroundColor = [UIColor whiteColor];

    //重定义返回键
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(0, 0, 22, 22);
    [back setImage:[UIImage imageNamed:@"iconfont-fanhui-white"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(BackAtTheNextHigherLevel) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backBar = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItem = backBar;
        
    //背景视图
    self.backgroundScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 48)];
    self.backgroundScrollView.backgroundColor = backgroundMainColor;
    [self.view addSubview:self.backgroundScrollView];
    self.backgroundScrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight * 1.1);
    [self.view addSubview:self.backgroundScrollView];

    //取车时间戳
    self.checkOutTimeStamp = [self.checkOutCarTimeStr timeIntervalSince1970];
    //还车时间戳
    self.giveBackTimeStamp = [self.giveBackCarTimeStr timeIntervalSince1970];
    

    
    //添加车辆信息
    [self carDetailsView];
    
    //添加订单信息
    [self orderDetailsView];

    //添加费用明细
    [self ExpensesDetailView];
    
    
    //确认下单按钮
    UIButton *confirmationOrderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmationOrderBtn.frame = CGRectMake(20, kScreenHeight - 41, kScreenWidth - 40, 34);
    confirmationOrderBtn.layer.cornerRadius = 3;
    confirmationOrderBtn.layer.masksToBounds = YES;
    confirmationOrderBtn.backgroundColor = mainColor;
    [confirmationOrderBtn setTitle:@"确认下单" forState:UIControlStateNormal];
    
    confirmationOrderBtn.titleLabel.tintColor = [UIColor whiteColor];
    [confirmationOrderBtn addTarget:self action:@selector(confirmationOrderPush) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmationOrderBtn];

    UIView *spaceViewThree = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 48, kScreenWidth, 0.5)];
    spaceViewThree.backgroundColor = lineColor;
    [self.view addSubview:spaceViewThree];
    

    
}

//天数差
- (void) d_valueTimeStampCount {
    
    
    //时间戳差
    self.d_valueForTime = self.giveBackTimeStamp - self.checkOutTimeStamp;
    
    //秒数差
    NSInteger timeD_Value = self.d_valueForTime;
    
    
    //余数
    NSInteger yushuD_Value = timeD_Value % 86400;//取余
    
    if (yushuD_Value == 0) {
        
        //天数差
        self.fateD_Value = timeD_Value / 86400;//取整

    } else if (yushuD_Value != 0) {
        
        //天数差
        self.fateD_Value = timeD_Value / 86400 + 1;//取整

    }
    

    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    if (self.giveBackCarTimeStr) {
           self.useCarTimeShowLabel.text = [NSString stringWithFormat:@"%ld天 (%@ - %@)",self.fateD_Value, [dateFormatter stringFromDate:self.checkOutCarTimeStr], [dateFormatter stringFromDate:self.giveBackCarTimeStr]];
        
    } else {
        
        self.useCarTimeShowLabel.text = [NSString stringWithFormat:@"%ld天",self.fateD_Value];

    }
 
    
    NSInteger rentmember = [self.rent_member_money integerValue];
    NSInteger insurance = [self.insurance_unit_money integerValue];
    
    self.rentShowLabel.text = [NSString stringWithFormat:@"%ld元", rentmember * self.fateD_Value];
    self.expensesDetailShowLabel.text = [NSString stringWithFormat:@"%ld元", rentmember * self.fateD_Value + insurance];

}

#pragma mark车辆信息
//车辆信息
- (void) carDetailsView{
    
    //第一部分 车辆信息
    UIView *carDetails = [[UIView alloc] initWithFrame:CGRectMake(5, 15, kScreenWidth - 10, 70)];
    carDetails.layer.cornerRadius = 2;
    carDetails.layer.shadowOpacity = 1.0;
    carDetails.layer.shadowOffset = CGSizeMake(0.0, 0.0);
    carDetails.layer.shadowColor = [UIColor colorWithRed:215 / 255.0 green:215 / 255.0 blue:215 / 255.0 alpha:1].CGColor;
    carDetails.layer.shadowRadius = 1;
    carDetails.backgroundColor = [UIColor whiteColor];
    [self.backgroundScrollView addSubview:carDetails];
    
    //车辆信息跳转按钮
    self.carDetailBtn = [DIYCarDetailButton buttonWithType:UIButtonTypeCustom];
    self.carDetailBtn.frame = CGRectMake(0, 0, kScreenWidth - 10, 35);
    self.carDetailBtn.layer.cornerRadius = 2;
    self.carDetailBtn.layer.masksToBounds = YES;
    [self.carDetailBtn addTarget:self action:@selector(carDetailBtnPush) forControlEvents:UIControlEventTouchUpInside];
    [carDetails addSubview:self.carDetailBtn];
    
    //车品牌
    self.carNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 25, 35)];
    self.carNameLabel.textAlignment = NSTextAlignmentCenter;
    self.carNameLabel.font = [UIFont systemFontOfSize:12];
    self.carNameLabel.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
    self.carNameLabel.text = self.brand_str;
    [carDetails addSubview:self.carNameLabel];
    
    //牌照
    self.licensePlateLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 35, 80, 35)];
    self.licensePlateLabel.textAlignment = NSTextAlignmentCenter;
    self.licensePlateLabel.font = [UIFont systemFontOfSize:12];
    self.licensePlateLabel.textColor = mainColor;
    self.licensePlateLabel.text = self.place_str;
    [carDetails addSubview:self.licensePlateLabel];
    
    //型号
    self.modelLabel = [[UILabel alloc] initWithFrame:CGRectMake(145, 45, 40, 15)];
    self.modelLabel.backgroundColor = [UIColor orangeColor];
    self.modelLabel.layer.cornerRadius = 2;
    self.modelLabel.layer.masksToBounds = YES;
    self.modelLabel.textAlignment = NSTextAlignmentCenter;
    self.modelLabel.font = [UIFont systemFontOfSize:12];
    self.modelLabel.textColor = [UIColor whiteColor];
    self.modelLabel.text = self.model_str;
    [carDetails addSubview:self.modelLabel];
    
    //自动或手动
    self.speed_boxLabel = [[UILabel alloc] initWithFrame:CGRectMake(205, 45, 40, 15)];
    self.speed_boxLabel.backgroundColor = [UIColor grayColor];
    self.speed_boxLabel.layer.cornerRadius = 2;
    self.speed_boxLabel.layer.masksToBounds = YES;
    self.speed_boxLabel.textAlignment = NSTextAlignmentCenter;
    self.speed_boxLabel.font = [UIFont systemFontOfSize:12];
    self.speed_boxLabel.textColor = [UIColor whiteColor];
    self.speed_boxLabel.text = self.speed_box;
    [carDetails addSubview:self.speed_boxLabel];

    
    //分割线
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 35, kScreenWidth - 20, 0.5)];
    lineLabel.backgroundColor = lineColor;
    [carDetails addSubview:lineLabel];

    
}

#pragma mark 订单信息
- (void) orderDetailsView{
    
    //第二部分 订单信息
    UIView *orderDetails = [[UIView alloc] initWithFrame:CGRectMake(5, 100, kScreenWidth - 10, 208)];
    orderDetails.layer.cornerRadius = 2;
    orderDetails.layer.shadowOpacity = 1.0;
    orderDetails.layer.shadowOffset = CGSizeMake(0.0, 0.0);
    orderDetails.layer.shadowColor = [UIColor colorWithRed:215 / 255.0 green:215 / 255.0 blue:215 / 255.0 alpha:1].CGColor;
    orderDetails.layer.shadowRadius = 1;
    orderDetails.backgroundColor = [UIColor whiteColor];
    [self.backgroundScrollView addSubview:orderDetails];

    UIImageView *orderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
    orderImageView.image = [UIImage imageNamed:@"icon-indent-blue"];
    [orderDetails addSubview:orderImageView];
    
    UILabel *orderInformation = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, 80, 20)];
    orderInformation.text = @"订单信息";
    orderInformation.textAlignment = NSTextAlignmentLeft;
    orderInformation.textColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1];
    orderInformation.font = [UIFont systemFontOfSize:14];
    [orderDetails addSubview:orderInformation];
    
    //用车时间标签
    UILabel *useCarTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, 70, 14)];
    useCarTimeLabel.text = @"用车时间:";
    useCarTimeLabel.textAlignment = NSTextAlignmentCenter;
    useCarTimeLabel.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
    useCarTimeLabel.font = [UIFont systemFontOfSize:14];
    [orderDetails addSubview:useCarTimeLabel];
    
    //用车时间显示标签
    self.useCarTimeShowLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 48, kScreenWidth / 5 * 3, 36)];
    self.useCarTimeShowLabel.numberOfLines = 0;
    self.useCarTimeShowLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.useCarTimeShowLabel.textAlignment = NSTextAlignmentLeft;
    self.useCarTimeShowLabel.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
    [self d_valueTimeStampCount];
    self.useCarTimeShowLabel.font = [UIFont systemFontOfSize:12];
    [orderDetails addSubview:self.useCarTimeShowLabel];
    
    //交车地点标签
    UILabel *exchangeAdressLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 94, 70, 14)];
    exchangeAdressLabel.text = @"交车地点:";
    exchangeAdressLabel.textAlignment = NSTextAlignmentCenter;
    exchangeAdressLabel.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
    exchangeAdressLabel.font = [UIFont systemFontOfSize:14];
    [orderDetails addSubview:exchangeAdressLabel];
    
    //交车地点显示标签
    self.car_addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 84, kScreenWidth / 5 * 3, 36)];
    self.car_addressLabel.text = self.car_address;
    self.car_addressLabel.numberOfLines = 0;
    self.car_addressLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.car_addressLabel.textAlignment = NSTextAlignmentLeft;
    self.car_addressLabel.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
    self.car_addressLabel.font = [UIFont systemFontOfSize:14];
    [orderDetails addSubview:self.car_addressLabel];
    

    //押金标签
    UILabel *cashPledgeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 128, 70, 14)];
    cashPledgeLabel.text = @"车辆押金:";
    cashPledgeLabel.textAlignment = NSTextAlignmentCenter;
    cashPledgeLabel.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
    cashPledgeLabel.font = [UIFont systemFontOfSize:14];
    [orderDetails addSubview:cashPledgeLabel];
    
    self.depositShowLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 128, kScreenWidth / 2, 14)];
    self.depositShowLabel.text = [NSString stringWithFormat:@"%@元", self.car_pledge_money];
    self.depositShowLabel.textAlignment = NSTextAlignmentLeft;
    self.depositShowLabel.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
    self.depositShowLabel.font = [UIFont systemFontOfSize:14];
    [orderDetails addSubview:self.depositShowLabel];
    
    //时间选择器
    //背景图片
    self.pickerImageView = [[UIView alloc] initWithFrame:CGRectMake(0, 162, kScreenWidth - 10, 46)];
    self.pickerImageView.backgroundColor = [UIColor clearColor];
    [orderDetails addSubview:self.pickerImageView];
    
    UIImageView *pickerImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 162, kScreenWidth - 10, 46)];
    pickerImage.image = [UIImage imageNamed:@"gary_bg"];
    [self.pickerImageView addSubview:pickerImage];
    [self choiceTimePickerView];
    
    //分割线
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 40, kScreenWidth - 20, 0.5)];
    lineLabel.backgroundColor = lineColor;
    [orderDetails addSubview:lineLabel];

}

#pragma mark 费用明细
- (void) ExpensesDetailView {
    
    //第三部分 费用明细
    UIView *expensesDetail = [[UIView alloc] initWithFrame:CGRectMake(5, 325, kScreenWidth - 10, 116)];
    expensesDetail.layer.cornerRadius = 2;
    expensesDetail.layer.shadowOpacity = 1.0;
    expensesDetail.layer.shadowOffset = CGSizeMake(0.0, 0.0);
    expensesDetail.layer.shadowColor = [UIColor colorWithRed:215 / 255.0 green:215 / 255.0 blue:215 / 255.0 alpha:1].CGColor;
    expensesDetail.layer.shadowRadius = 1;
    expensesDetail.backgroundColor = [UIColor whiteColor];
    [self.backgroundScrollView addSubview:expensesDetail];
    
    UIImageView *expensesImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 19, 20)];
    expensesImageView.image = [UIImage imageNamed:@"icon-cost"];
    [expensesDetail addSubview:expensesImageView];
    
    UILabel *expensesDetailLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 10, 80, 20)];
    expensesDetailLabel.text = @"费用明细";
    expensesDetailLabel.textAlignment = NSTextAlignmentLeft;
    expensesDetailLabel.textColor = [UIColor colorWithRed:51 / 255.0 green:51 / 255.0 blue:51 / 255.0 alpha:1];
    expensesDetailLabel.font = [UIFont systemFontOfSize:14];
    [expensesDetail addSubview:expensesDetailLabel];
    
    NSInteger rentmember = [self.rent_member_money integerValue];
    NSInteger insurance = [self.insurance_unit_money integerValue];
    
    self.expensesDetailShowLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - 170, 10, 150, 20)];
    self.expensesDetailShowLabel.text = [NSString stringWithFormat:@"%ld元", rentmember * self.fateD_Value + insurance];
    self.expensesDetailShowLabel.textColor = mainColor;
    self.expensesDetailShowLabel.textAlignment = NSTextAlignmentRight;
    self.expensesDetailShowLabel.font = [UIFont systemFontOfSize:12];
    [expensesDetail addSubview:self.expensesDetailShowLabel];
    

    UILabel *rentLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 56, 48, 16)];
    rentLabel.textColor = [UIColor colorWithRed:102 / 255.0 green:102 / 255.0 blue:102 / 255.0 alpha:1];
    rentLabel.text = @"租金:";
    rentLabel.textAlignment = NSTextAlignmentLeft;
    rentLabel.font = [UIFont systemFontOfSize:14];
    [expensesDetail addSubview:rentLabel];
    
    self.rentShowLabel = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - 170, 54, 150, 20)];
    self.rentShowLabel.text = [NSString stringWithFormat:@"%ld元", rentmember * self.fateD_Value];
    self.rentShowLabel.textColor = mainColor;
    self.rentShowLabel.textAlignment = NSTextAlignmentRight;
    self.rentShowLabel.font = [UIFont systemFontOfSize:10];
    [expensesDetail addSubview:self.rentShowLabel];

    
    UILabel *insuredAmount = [[UILabel alloc] initWithFrame:CGRectMake(40, 88, 48, 16)];
    insuredAmount.textColor = [UIColor colorWithRed:102 / 255.0 green:102 / 255.0 blue:102 / 255.0 alpha:1];
    insuredAmount.text = @"保险:";
    insuredAmount.textAlignment = NSTextAlignmentLeft;
    insuredAmount.font = [UIFont systemFontOfSize:14];
    [expensesDetail addSubview:insuredAmount];

    UILabel *insuredAmountShow = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth - 170, 86, 150, 20)];
    insuredAmountShow.text = [NSString stringWithFormat:@"%@元", self.insurance_unit_money];
    insuredAmountShow.textColor = mainColor;
    insuredAmountShow.textAlignment = NSTextAlignmentRight;
    insuredAmountShow.font = [UIFont systemFontOfSize:10];
    [expensesDetail addSubview:insuredAmountShow];

    
    //分割线
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 40, kScreenWidth - 20, 0.5)];
    lineLabel.backgroundColor = lineColor;
    [expensesDetail addSubview:lineLabel];
    
}

#pragma mark 确认下单跳转
- (void) confirmationOrderPush {
    
    [self NetworkRequests];
    
}


//返回上一级
- (void) BackAtTheNextHigherLevel {
    
    [self.navigationController popViewControllerAnimated:nil];
    
}

#pragma mark 时间选择器
- (void)choiceTimePickerView {
    
    //----------------------------时间选择器----------------------------------
    //取车时间按钮
    self.checkOutCarTimeBtn = [DIYChoiceTimeButton buttonWithType:UIButtonTypeCustom];
    self.checkOutCarTimeBtn.backgroundColor = [UIColor clearColor];
    self.checkOutCarTimeBtn.frame = CGRectMake( 0, 0, kScreenWidth / 2 - 5, 50);
    self.checkOutCarTimeBtn.useCarTimeLabel.text = @"取车时间:";
    [self.checkOutCarTimeBtn addTarget:self action:@selector(checkOutCarTimePush) forControlEvents:UIControlEventTouchUpInside];
    [self.pickerImageView addSubview:self.checkOutCarTimeBtn];
    if (self.checkOutCarTimeStr) {
        NSDateFormatter *dateFormatter0 = [[NSDateFormatter alloc] init];
        [dateFormatter0 setDateFormat:@"MM-dd HH:mm"];
        
        NSString *strDate0 = [dateFormatter0 stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:self.checkOutCarTimeStr]];
        
        self.checkOutCarTimeBtn.timeShowLabel.text = strDate0;
        self.checkOutCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-blue"];

    }

    
    //还车时间按钮
    self.giveBackCarTimeBtn = [DIYChoiceTimeButton buttonWithType:UIButtonTypeCustom];
    self.giveBackCarTimeBtn.backgroundColor = [UIColor clearColor];
    self.giveBackCarTimeBtn.frame = CGRectMake(kScreenWidth / 2 - 5, 0, kScreenWidth / 2 - 5, 50);
    self.giveBackCarTimeBtn.userInteractionEnabled = NO;
    self.giveBackCarTimeBtn.useCarTimeLabel.text = @"还车时间:";
    [self.giveBackCarTimeBtn addTarget:self action:@selector(giveBackCarTimePush) forControlEvents:UIControlEventTouchUpInside];
    [self.pickerImageView addSubview:self.giveBackCarTimeBtn];
    
    if (self.giveBackCarTimeStr) {
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
        NSString *strDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:self.giveBackCarTimeStr]];
        self.giveBackCarTimeBtn.userInteractionEnabled = YES;
        self.giveBackCarTimeBtn.timeShowLabel.text = strDate;
        self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-blue"];

    }

    //------------------------------此处以上-------------------------------------

}

#pragma mark 时间选择器弹出
//取车时间
- (void) checkOutCarTimePush {

    self.datePickerView = [[DIYDatePickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    [self.datePickerView didFinishSelectedDate:^(NSDate *selectDateTime) {
        
        if (selectDateTime) {
            
            if (self.giveBackCarTimeStr) {

                self.checkOutCarTimeStr = selectDateTime;
                
                self.checkOutTimeStamp = [selectDateTime timeIntervalSince1970];
                
                self.giveBackTimeStamp = [self.giveBackCarTimeStr timeIntervalSince1970];
                
                self.d_valueForTime = self.giveBackTimeStamp - self.checkOutTimeStamp;
                

                if (self.d_valueForTime <= 0) {
                    //时间差小于0 重置还车时间

                    //初始化提示框；
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"还车时间小于取车时间,还车时间重置" preferredStyle:  UIAlertControllerStyleAlert];
                    
                    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        //点击按钮的响应事件；
                        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
                        
                        NSString *strDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:selectDateTime]];
                        
                        self.checkOutCarTimeBtn.timeShowLabel.text = strDate;
                        
                        //取车时间
                        self.checkOutCarTimeStr = selectDateTime;
                        //取车时间戳
                        self.checkOutTimeStamp = [selectDateTime timeIntervalSince1970];
                        
                        //更换时间图标
                        self.checkOutCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-blue"];
                        
                        //打开取车时间交互
                        self.giveBackCarTimeBtn.userInteractionEnabled = YES;
                        
                        //还车时间
                        self.giveBackCarTimeStr = nil;
                        self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];
                        self.giveBackCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
                        
                    }]];
                    
                    //弹出提示框；
                    [self presentViewController:alert animated:true completion:nil];
                    
                } else if (self.d_valueForTime > 7776000) {
                    
                    //初始化提示框；
                    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"租车时间不能超过90天,还车时间重置" preferredStyle:  UIAlertControllerStyleAlert];
                    
                    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        //点击按钮的响应事件；
                        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                        [dateFormatter setDateFormat:@"MM-dd HH:mm"];
                        
                        NSString *strDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:selectDateTime]];
                        
                        self.checkOutCarTimeBtn.timeShowLabel.text = strDate;
                        
                        //取车时间
                        self.checkOutCarTimeStr = selectDateTime;
                        //取车时间戳
                        self.checkOutTimeStamp = [selectDateTime timeIntervalSince1970];
                        
                        //更换时间图标
                        self.checkOutCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-blue"];
                        
                        //打开取车时间交互
                        self.giveBackCarTimeBtn.userInteractionEnabled = YES;
                        
                        //还车时间
                        self.giveBackCarTimeStr = nil;
                        self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];
                        self.giveBackCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
                        
                    }]];
                    
                    //弹出提示框；
                    [self presentViewController:alert animated:true completion:nil];

                } else {
                    
                    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat:@"MM-dd HH:mm"];
                    
                    NSString *strDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:selectDateTime]];
                    
                    self.checkOutCarTimeBtn.timeShowLabel.text = strDate;
                    
                    //取车时间
                    self.checkOutCarTimeStr = selectDateTime;
                    //取车时间戳
                    self.checkOutTimeStamp = [selectDateTime timeIntervalSince1970];
                    
                    //更换时间图标
                    self.checkOutCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-blue"];
                    
                    //打开取车时间交互
                    self.giveBackCarTimeBtn.userInteractionEnabled = YES;
                                        
                }
                
            } else {
                
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"MM-dd HH:mm"];
                
                NSString *strDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:selectDateTime]];
                
                self.checkOutCarTimeBtn.timeShowLabel.text = strDate;
                
                //取车时间
                self.checkOutCarTimeStr = selectDateTime;
                //取车时间戳
                self.checkOutTimeStamp = [selectDateTime timeIntervalSince1970];
                
                //更换时间图标
                self.checkOutCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-blue"];
                
                //打开取车时间交互
                self.giveBackCarTimeBtn.userInteractionEnabled = YES;
                
                
            }
            
   
            
        } else {
            
            //取车时间
            self.checkOutCarTimeStr = nil;
            self.checkOutCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];
            self.checkOutCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
            //还车时间
            self.giveBackCarTimeStr = nil;
            self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];
            self.giveBackCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
            self.giveBackCarTimeBtn.userInteractionEnabled = NO;

            //时间计算
            [self d_valueTimeStampCount];

        }
        
        
    }];
    
    
}

//还车时间
- (void) giveBackCarTimePush {

    self.giveBackDatePickerView = [[DIYGiveBackDatePickerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    [self.giveBackDatePickerView didFinishSelectedDate:^(NSDate *selectDateTime) {
        
        if (selectDateTime) {
            
            //还车时间
            self.giveBackCarTimeStr = selectDateTime;
            //还车时间戳
            self.giveBackTimeStamp = [selectDateTime timeIntervalSince1970];
            //时间戳差
            self.d_valueForTime = self.giveBackTimeStamp - self.checkOutTimeStamp;
            
            if (self.d_valueForTime <= 0 ) {
                
                [self timeDvalueAnalysis:1];
                
            } else if (self.d_valueForTime >7776000) {
                
                [self timeDvalueAnalysis:2];
            } else {
                
                NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                [dateFormatter setDateFormat:@"MM-dd HH:mm"];
                
                NSString *strDate = [dateFormatter stringFromDate:[NSDate dateWithTimeInterval:0 sinceDate:selectDateTime]];
                
                self.giveBackCarTimeBtn.timeShowLabel.text = strDate;
                
                //更换时间图标
                self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-blue"];
                
                NSDateFormatter *dareFormatter1 = [[NSDateFormatter alloc] init];
                [dareFormatter1 setDateFormat:@"yyyyMMddHHmm"];
                
                [self d_valueTimeStampCount];

            }
            
            
        } else {
            
            //还车时间
            self.giveBackCarTimeStr = nil;
            self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];
            self.giveBackCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
            
        }
        
    }];
    
}

//--------------------------------此处以上-----------------------------------------

//车辆信息跳转
- (void) carDetailBtnPush {
    
    CarDetailForgeViewController *carDetailForgeVC = [[CarDetailForgeViewController alloc] init];
    carDetailForgeVC.carIDStr = self.car_id;
    
    [self.navigationController pushViewController:carDetailForgeVC animated:nil];
    
    
}


#pragma mark 时间控制分析
- (void) timeDvalueAnalysis:(NSInteger) status {
    
    
    
    switch (status) {
        case 1:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"还车时间必须大于用车时间" preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
                self.giveBackCarTime = nil;
                self.giveBackCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
                
                //更换时间图标
                self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 2:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"最长租期不能大于90天" preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
                
                //点击按钮的响应事件；
                self.giveBackCarTime = nil;
                self.giveBackCarTimeBtn.timeShowLabel.text = @"-- / -- / --";
                
                self.giveBackTimeStamp = [self.giveBackCarTime timeIntervalSince1970];
                //更换时间图标
                self.giveBackCarTimeBtn.timeChoiceImageView.image = [UIImage imageNamed:@"iconfont-shijian-gray"];
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
            
        default:
            break;
    }
    
    
}


#pragma mark 网络请求
- (void) NetworkRequests{
    
    //初始化进度框，置于当前的View当中
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.label.text = @"正在加载";
    [HUD showAnimated:YES];//HUD开始
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    //申明返回的结果是json类型
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //申明请求的数据是json类型
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    //如果报接受类型不一致请替换一致text/html或别的
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    Access_token *useAccess = [Access_token shareInstance];
    
    NSString *openTime = [NSString stringWithFormat:@"%.0f", self.checkOutTimeStamp];
    NSString *endTime = [NSString stringWithFormat:@"%.0f", self.giveBackTimeStamp];
    
    //传入的参数
    NSDictionary *para = @{
                           @"type" : @"1",
                           @"access_token" : useAccess.access_token,
                           @"car_id" : self.car_id,
                           @"open_time" : openTime,
                           @"end_time" : endTime
                           };
    //你的接口地址
    
    
    NSString *url = [NSString stringWithFormat:@"%@%@", kRealmName,kNetOrderAddOrder];
    //发送请求
    [manager POST:url parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        [HUD hideAnimated:YES];
        
        
        NSDictionary *dic = responseObject;
        
        self.status = dic[@"status"];
        
        self.msg = dic[@"msg"];
        
        NSDictionary *info = dic[@"info"];
        
        self.ordersID = info[@"id"];
        
        
        //返回值分析
        [self resultAnalysis];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [HUD hideAnimated:YES];
        
        //初始化提示框；
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有网络" preferredStyle:  UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //点击按钮的响应事件；
        }]];
        
        //弹出提示框；
        [self presentViewController:alert animated:true completion:nil];

        
    }];
    
}


#pragma mark 返回值分析
- (void) resultAnalysis {
    
    int status = [self.status intValue];
    
    switch (status) {
        case 99:{
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 98:{
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 97:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 96:
        {
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            
            break;
        case 1:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 2:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                //点击按钮的响应事件；
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
        }
            break;
        case 3:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 4:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 5:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 6:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 7:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 8:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 9:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 10:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 11:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 12:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 13:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;
        case 14:
        {
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
            
            
        }
            break;

        case 100:
        {
            
            
            //初始化提示框；
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:self.msg preferredStyle:  UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                //点击按钮的响应事件；
                
                //跳转到支付界面
                [self pushToPaymentVC];

 
            }]];
            
            //弹出提示框；
            [self presentViewController:alert animated:true completion:nil];
         }
            break;
            
        default:
            break;
    }
    
    
    
}

//跳转到支付界面
- (void) pushToPaymentVC {
    
    PaymentViewController *paymentVC = [[PaymentViewController alloc] init];
    paymentVC.rentOrderPaymentModel = self.rentOrderPaymentModel;
    
    paymentVC.orderID = self.ordersID;
    
    [self.navigationController pushViewController:paymentVC animated:NO];
    
}

@end
